﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using StockholmsStad.Windows.UI.Modules.Maintenance.BusinessEntities;
using StockholmsStad.Windows.UI.Modules.Maintenance.Controls;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Views
{
    /// <summary>
    /// Interaction logic for GeneratedMonthsControl.xaml
    /// </summary>
    public partial class GeneratedMonthsView
    {
        private readonly IMaintenanceService maintenanceService;
        public GeneratedMonthsView(IMaintenanceService service)
        {
            maintenanceService = service;
            InitializeComponent();
        }

        public void UnloadView()
        {
            var numberOfChilds = stackPanelDates.Children.Count;
            var element = stackPanelDates.Children[numberOfChilds - 1];

            if (element.GetType().Equals(typeof(GeneratedDaysControl)))
            {
                stackPanelDates.Children.RemoveAt(numberOfChilds - 1);
                listViewMonths.InvalidateVisual();
            }
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            listViewMonths.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(listViewMonths_MouseLeftButtonDown), true);
        }

        private void listViewMonths_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var view = sender as ListView;

            if (view != null)
            {
                var month = view.SelectedItem as ScheduledMonth;

                if (month != null)
                {
                    maintenanceService.GetMinisters(month.TimeBlockPresentationModel, month.Date);

                    int numberOfChilds = stackPanelDates.Children.Count;
                    var element = stackPanelDates.Children[numberOfChilds - 1];

                    if (element.GetType().Equals(typeof (GeneratedDaysControl)))
                    {
                        // näst sista elementet är dagkontrollen (knapparna är sista elementen)
                        var dl = element as GeneratedDaysControl;
                        if (dl != null)
                        {
                            dl.Model = month.TimeBlockPresentationModel;
                            stackPanelDates.Children[numberOfChilds - 1] = dl;
                        }
                    }
                    else
                    {
                        // ingen dagkontroll existerar, skapa ny
                        var dayList = new GeneratedDaysControl
                                          {
                                              Model = month.TimeBlockPresentationModel,                                              
                                          };

                        stackPanelDates.Children.Add(dayList);
                    }

                }
            }
        }
    }
}
